package com.base.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.dao.UserDao;
import com.base.mapper.UserMapper;
import com.base.model.User;
import com.base.plugin.memcached.Memcached;

@Service
public class UserService {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserMapper userMapper;
	
	@SuppressWarnings({"unchecked", "rawtypes"})
	public User addUser(String username, String password) {
		Map map = new HashMap();
		map.put("username", username);
		map.put("password", password);
		userDao.insert(map);
		return userDao.selectById(1);
	}
	
	public User getUserById(int id) {
		return userMapper.selectById(id);
	}
	
	public User getCacheUserById(int id) {
		String key = "user:" + id;
		User user = (User)Memcached.use().get(key);
		if(user==null) {
			user = userDao.selectById(id);
			if(user!=null) {
				Memcached.use().set(key, user);
			}
		}
		return user;
	}

}
